package com.example.blogdemo.common.Utils;

import jakarta.validation.constraints.NotBlank;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.nio.charset.StandardCharsets;
import java.util.UUID;

public class SecurityUtils {

    public static String entrity(@NotBlank(message = "密码不能为空") String password) {

        String salt = UUID.randomUUID().toString().replace("-", "");
        String s = DigestUtils.md5DigestAsHex((salt + password).getBytes(StandardCharsets.UTF_8));
        return salt + s;

    }

    public static Boolean checkPassword(@NotBlank(message = "密码不能为空") String password, String blogpassword) {



        if (blogpassword == null || blogpassword.length() != 64) {
            return false;
        }
        String slat = blogpassword.substring(0, 32);
        String s = DigestUtils.md5DigestAsHex((slat + password).getBytes(StandardCharsets.UTF_8));
        return (slat + s).equals(blogpassword);


    }
}
